var caba = new google.maps.LatLng(-34.598124, -58.402436);
var map;
var markers = [];
var afiliadoMarker;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();

function initialize() {
	//alert("initialize");	
	var input = document.getElementById('direccionAfiliado');
    var autocomplete = new google.maps.places.Autocomplete(input);
	
	map = new google.maps.Map(document.getElementById('map_canvas'), {
			zoom: 11,
			center: caba,
			mapTypeId: google.maps.MapTypeId.ROADMAP
			});

	autocomplete.bindTo('bounds', map);
	google.maps.event.addListener(autocomplete, 'place_changed', function() {
		infowindow.close();
		var place = autocomplete.getPlace();
		if (place.geometry.viewport) {
			map.fitBounds(place.geometry.viewport);
		} else {
			addAfiliadoMarker(place.geometry.location);
			bounds = new google.maps.LatLngBounds();
			bounds.extend(place.geometry.location);
			for(i in markers){
				bounds.extend(markers[i].getPosition());
			}
			map.fitBounds(bounds);
		}
	});
	    
	geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': document.getElementById("direccionAfiliado").value+", Argentina"}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {	
        	addAfiliadoMarker(results[0].geometry.location);
        	map.setZoom(17);
        } else {
        	alert("No se pudo geolocalizar el Afiliado.");
        }
    });
}

function deleteOverlays() {
	if (markers) {
		for (i in markers) {
			markers[i].setMap(null);
		}
		markers.length = 0;
	}
}

function addAfiliadoMarker(location){
	if(afiliadoMarker == null){
		bounds.extend(location);
		map.fitBounds(bounds);
		afiliadoMarker = new google.maps.Marker({
			map: map,
			icon: "../img/iconPeople.png"
		});
	}
	var nombreAfiliado = document.getElementById("nombreAfiliado").value;
	//var valorAfiliadoSplit = document.getElementById("direccionAfiliado").value.split(",");
	var divTag = document.createElement("div");
    divTag.id = "div1";
    divTag.innerHTML = nombreAfiliado + "</br>" + document.getElementById('direccionAfiliado').value;
	google.maps.event.addListener(	afiliadoMarker, 
									'click', 
									function() {
										infowindow.setContent(divTag);
										infowindow.open(map, afiliadoMarker);
									});
	
	afiliadoMarker.setPosition(location);
	// Add a Circle overlay to the map.
	var circle = new google.maps.Circle({
						map: map,
						radius: 3000 // 3000 m
					});
	circle.bindTo('center', afiliadoMarker, 'position');
}

function addLocation (latitud, longitud, datos, nombre, email){
	var latLng = new google.maps.LatLng(latitud, longitud);
	var marker = new google.maps.Marker({
	      				position: latLng,
	      				map: map,
	      				icon: "../img/iconyellow.png",
	      				draggable: false,
	      				animation: google.maps.Animation.DROP
	    			});
	markers.push(marker);
	google.maps.event.addListener(	marker, 
									'click', 
									function() {
										buscar_marker(marker);
										infowindow.close();
										infowindow.setContent(datos);
										infowindow.open(map, marker);
										postVerCalendarioSubmit (email, nombre);
									});
	bounds.extend(latLng);
	map.fitBounds(bounds);
	//alert("addLocation");
}

function openPopUp(url){
	mywindow = window.open(url, "mywindow", "location=1,status=1,scrollbars=1");
    mywindow.moveTo(0, 0);
}

function buscar_marker(markerSrc) {
	//alert("buscar_marker");
	for ( var i in markers) {
		var marker = markers[i];
		if (marker.getPosition().equals(markerSrc.getPosition())){
			marker.setIcon(new google.maps.MarkerImage("../img/iconred.png"));
		}else{
			marker.setIcon(new google.maps.MarkerImage("../img/iconyellow.png"));
		}
	}
}

function postVerCalendario() {
	var value = document.getElementById('admisoresSelect').options[document.getElementById('admisoresSelect').selectedIndex].value;
	if (value != null && value != 'null') {
		var valueSplit = value.split(";");
		postVerCalendarioSubmit(valueSplit[0], valueSplit[1]);
		var markerAUX = new google.maps.Marker({position: new google.maps.LatLng(valueSplit[2], valueSplit[3])});
		buscar_marker(markerAUX);
	}
}

function postVerCalendarioSubmit (admisoresEmail, admisorNombre) {
	document.getElementById("admisor").value=admisoresEmail; 
	document.getElementById("admisorNombre").value=admisorNombre; 
	document.getElementById("htmlPostVerCalendario").click();
}
